import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/login/Login'
import Homepage from '@/components/login/Homepage'
import Backstage from '@/components/login/Backstage'
import selepurch from '@/components/selepurch'
import test1 from '@/components/test1'
import welcome from '@/components/login/welcome'
import exorder from '@/components/login/exorder'
import detailed from '@/components/login/detailed'
import inventory from '@/components/login/inventory'
import factory from '@/components/login/factory'
 
Vue.use(Router)
 
const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/',
      name: 'Homepage',
      component: Homepage
    },
    {
      path: '/Backstage',
      name: 'Backstage',
      component: Backstage
    },
    {
      path: '/selepurch',
      component: selepurch,
      redirect:'/welcome',
      children:[{
        path:'/welcome',
        component:welcome
      },
      {
        path:'/exorder',
        component: exorder
      },
      {
        path:"/detailed",
        component: detailed
      },
      {
        path:"/inventory",
        component: inventory,
      },
      {
        path:"/factory",
        component: factory,
      },
    ]
    },
    {
      path: '/test1',
      name: 'test1',
      component: test1
    }
  ]
})

router.beforeEach((to, from, next) => {
  if(to.path == '/test1') return next();
  if(to.path == '/Backstage') return next();
  if(to.path == '/Homepage') return next();
  if(to.path == '/login') return next();

  const limitsStr = window.sessionStorage.getItem("limits");
  const jurStr = window.sessionStorage.getItem("jur");
  if (limitsStr != 1) return next('/login');
  if (jurStr != 1 && to.path == '/factory') return next('/selepurch');
  next();
})

export default router
